"""Add performance monitoring tables.

Revision ID: 20251018_1550
Revises: 20251018_1549
Create Date: 2025-10-18 15:50:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20251018_1550'
down_revision = '20251018_1549'
depends_on = None


def upgrade() -> None:
    """Create performance_records table."""
    op.create_table(
        'performance_records',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('device_id', sa.Integer(), nullable=False),
        sa.Column('metric_name', sa.String(255), nullable=False),
        sa.Column('operation', sa.String(50), nullable=False),
        sa.Column('duration', sa.Float(), nullable=False),
        sa.Column('success', sa.Boolean(), nullable=False),
        sa.Column('error_message', sa.String(500), nullable=True),
        sa.Column('timestamp', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
        sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for performance_records
    op.create_index(op.f('ix_performance_records_device_id'), 'performance_records', ['device_id'])
    op.create_index(op.f('ix_performance_records_id'), 'performance_records', ['id'])
    op.create_index(op.f('ix_performance_records_timestamp'), 'performance_records', ['timestamp'])
    op.create_index('idx_perf_device_metric_time', 'performance_records', ['device_id', 'metric_name', 'timestamp'])


def downgrade() -> None:
    """Drop performance_records table."""
    op.drop_index('idx_perf_device_metric_time', table_name='performance_records')
    op.drop_index(op.f('ix_performance_records_timestamp'), table_name='performance_records')
    op.drop_index(op.f('ix_performance_records_id'), table_name='performance_records')
    op.drop_index(op.f('ix_performance_records_device_id'), table_name='performance_records')
    op.drop_table('performance_records')
